﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class EditAdjustSpecial : BasePage
    {
        private IPolicySchemeBLL policySchemeBLL = IocContainer.Instance.Resolve<IPolicySchemeBLL>();
        private IWeekLongBLL weekLongBLL = IocContainer.Instance.Resolve<IWeekLongBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtAStartDate.Attributes.Add("readonly", "true");
                this.txtAEndDate.Attributes.Add("readonly", "true");
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int id = 0;
                    int.TryParse(Request.QueryString["Id"], out id);
                    if (id > 0)
                    {
                        PolicyScheme scheme = policySchemeBLL.GetPolicyById(id);
                        this.txtSchemeName.Text = scheme.SchemeName;
                        this.txtSchemeCode.Text = scheme.SchemeCode;
                        //this.ddlPriceType.SelectedValue = scheme.PriceType;
                        if (scheme.PriceType == "1")
                        {
                            this.ddlPriceType.Text = "离岸价";
                        }
                        else if (scheme.PriceType == "2")
                        {
                            this.ddlPriceType.Text = "到岸价";
                        }
                        else
                        {
                            this.ddlPriceType.Text = "";
                        }

                        this.txtPrice.Text = scheme.Price.ToString();
                        this.txtTaxPrice.Text = scheme.TaxPrice.ToString();
                        this.txtUnit.Text = scheme.Unit;
                        this.txtStartDate.Text = scheme.StartTime != null ? scheme.StartTime.Value.ToString("yyyy-MM-dd") : "";
                        this.txtEndDate.Text = scheme.EndTime != null ? scheme.EndTime.Value.ToString("yyyy-MM-dd") : "";
                        //this.ddlSpecialType.SelectedValue = (Convert.ToInt16(scheme.IsSpecialType)).ToString();
                        if (scheme.IsSpecialType)
                        {
                            this.ddlSpecialType.Text = "是";
                        }
                        else
                        {
                            this.ddlSpecialType.Text = "否";
                        }

                        this.txtProductId.Value = scheme.ProductId.ToString();
                        this.txtProductName.Text = scheme.ProductName;
                        this.txtTaxRatio.Text = scheme.TaxRatio.ToString();
                        //this.txtTaxPrice.Attributes.Add("readonly", "readonly");
                        this.txtAdjustCode.Text = DateTime.Now.ToString("yyyymmddhhss");
                        this.txtAdjustCode.Attributes.Add("readonly", "readonly");
                    }
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Special wldm = new Special();
                SpecialEntry ewldm = new SpecialEntry();

                wldm.FNumber = this.txtAdjustCode.Text;
                wldm.FPricelistID = int.Parse(Request.QueryString["Id"].ToString());
                wldm.FMaker = int.Parse(UserID);
                wldm.Fdate = DateTime.Now;
                wldm.FBeginDate = Convert.ToDateTime(this.txtAStartDate.Text);
                wldm.FEndinDate = Convert.ToDateTime(this.txtAEndDate.Text);

                ewldm.FEntryID = 1;
                ewldm.FOLDPrice = Convert.ToDecimal(this.txtPrice.Text);
                ewldm.FNEWPrice = Convert.ToDecimal(this.txtAdjustPrice.Text);
                ewldm.FTaxNewPrice = Convert.ToDecimal(this.txtTaxAdjustPrice.Text);
                ewldm.FProductid = int.Parse(Request.QueryString["ProductID"].ToString());
                ewldm.FisSet = Convert.ToInt32(this.ddlFisSet.SelectedValue);
                if (this.ddlFisSet.SelectedValue == "1")
                {
                    ewldm.FSetQty = int.Parse(this.txtFSetQty.Text);
                }
                else
                {
                    ewldm.FSetQty = 0;
                }

                weekLongBLL.AddSpecial(wldm, ewldm);

                ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");

            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
